//
//  ViewController2.m
//  savedata
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController2.h"
#import "studentModel.h"

@interface ViewController2 ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *stuList;
@end

@implementation ViewController2

- (NSMutableArray *)stuList
{
    if (_stuList == nil)
    {
        NSFileManager *fm = [NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self dataPath]])
        {
            NSData *data = [NSData dataWithContentsOfFile:[self dataPath]];
            NSKeyedUnarchiver *unArch = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
            _stuList = [unArch decodeObjectForKey:@"Students"];
            [unArch finishDecoding];
           
        }
        else
        {
            _stuList = [NSMutableArray array];
        }
    }
    return _stuList;
}

- (NSString *)dataPath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingString:@"Documents"];
    NSString *filePath = [docPath stringByAppendingString:@"students1.plist"];
    return filePath;
}

- (void)savedata
{
    NSMutableData *mdata = [NSMutableData data];
    NSKeyedArchiver *arch = [[NSKeyedArchiver alloc]initForWritingWithMutableData:mdata];
    [arch encodeObject:self.stuList forKey:@"Students"];
    [arch finishEncoding];
    [mdata writeToFile:[self dataPath] atomically:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)add:(id)sender
{
    studentModel *m = [[studentModel alloc] initWithId:self.IDField.text
                                               andName:self.nameField.text
                                                andAge:self.ageField.text];
    [self.stuList addObject:m];
    [self savedata];
    [self.tableview reloadData];
}
- (IBAction)delete:(id)sender
{
    for (studentModel  *tmp in self.stuList )
    {
        if ([tmp.stuId isEqualToString:self.IDField.text]) {
            [self.stuList removeObject:tmp];
            break;
        }
    }
    [self savedata];
    [self.tableview reloadData];
}
- (IBAction)modify:(id)sender
{
    for (studentModel  *tmp in self.stuList )
    {
        if ([tmp.stuId isEqualToString:self.IDField.text]) {
            tmp.stuName = self.nameField.text;
            tmp.stuAge = self.ageField.text;
            break;
        }
    }
    [self savedata];
    [self.tableview reloadData];

    
}
- (IBAction)find:(id)sender
{
    for (studentModel  *tmp in self.stuList )
    {
        if ([tmp.stuId isEqualToString:self.IDField.text])
        {
            self.nameField.text = tmp.stuName ;
            self.ageField.text = tmp.stuAge ;
            break;
        }
        else
        {
            self.nameField.text = @"找不到数据" ;
            self.ageField.text = @"找不到数据" ;
            break;
            
        }
    }

}
- (IBAction)findall:(id)sender
{
    for (studentModel  *tmp in self.stuList )
    {
        NSLog(@"%@",tmp);
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.stuList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    studentModel *dict = self.stuList[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@ %@",dict.stuId,dict.stuName,dict.stuAge];
//    NSLog(@"%@ %@ %@",dict[@"stuId"],dict[@"stuName"],dict[@"stuAge"]);
    return cell;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
